* This produces Table A.2 and Figure A.7 in the Internet Appendix.
clear all
use ../data/fig_a7_data.dta,clear

foreach var in pop medinc density{	
    cap gen ln`var' = ln(`var')
}

gen bachelorsshr=bachelors/pop

global X "lnpop lnmedinc bachelorsshr lndensity"


g chinesepct=chineseper*100 //in percentage points//
sum chinesepct, detail
xtile chn_pctile = chinesepct, nq(100)

sum chinesepct, detail
gen treat = (chinesepct>=r(p90))

label var chineseper "CHShare"
label var treat "Top Chn Region"
label var chn_pctile "CHPercentile"

*regression table
local i=1
foreach var in d_lnemp_0107 d_lnincome_0107 d_lnemp_9600 d_lnincome_9801 {
	qui reghdfe `var' chineseper $X, absorb(countyname) cluster(zipcode)
	eststo reg`i'
	estadd local ctrl "Yes"
	estadd local cfe "Yes"
	qui reghdfe `var' chn_pctile $X, absorb(countyname) cluster(zipcode)
	eststo reg`i'_1
	estadd local ctrl "Yes"
	estadd local cfe "Yes"
	local ++i
}
esttab reg* using "../results/tableA2.tex", replace /// 
	keep(chineseper chn_pctile)  ///
	order(chineseper chn_pctile) ///
	label ///
	star(* 0.10 ** 0.05 *** 0.01) /// 
	cells(b(star fmt(%9.4f)) se(par fmt(%9.4f))) ///
	stats(ctrl cfe N, ///
		fmt(0 0 %11.0gc) ///
		labels( "Controls" ///
		    "County FE" /// 
			"Obs." ///
			)) ///			
	mlabels(Emp0107 Emp0107 Inc0107 Inc0107 Emp9600 Emp9600 Inc9600 Inc9600) collabels(, none)  
eststo clear

****graph*****
set scheme s2color
grstyle init
grstyle color ci_area gs12%50
grstyle color ci_arealine gs12%0

preserve
reghdfe d_lnemp_0107 $X, absorb(countyname) res(empres)
collapse empres, by(chn_pctile)
graph twoway (lfitci empres chn_pctile,clstyle(p2line)) (scatter empres chn_pctile, msymbol(o)msize(small) mcolor(navy)), ///
			  title("") xtitle("Chinese Population Share in 2000, by Percentile",size(8pt)) ytitle("Pre-shock Employment, 2001-2007",size(8pt))  ///
			  xlab(,labsize(small)) ylab(,labsize(small)) ///
			  graphregion(color(white)) legend(off) /// 
			  saving("../results/emp_chnperc1.gph",replace) 
restore

preserve
reghdfe d_lnincome_0107 $X, absorb(countyname) res(incres)
collapse incres, by(chn_pctile)
graph twoway (lfitci incres chn_pctile,clstyle(p2line)) (scatter incres chn_pctile, msymbol(o) msize(small) mcolor(navy)), ///
			  title("") xtitle("Chinese Population Share in 2000, by Percentile",size(8pt)) ytitle("Pre-shock Income, 2001-2007",size(8pt)) ///
			  xlab(,labsize(small)) ylab(,labsize(small)) ///
			  graphregion(color(white)) legend(off) ///
			  saving("../results/inc_chnperc1.gph",replace) 
restore

preserve
reghdfe d_lnemp_9600 $X, absorb(countyname) res(empres)
collapse empres, by(chn_pctile)
graph twoway (lfitci empres chn_pctile,clstyle(p2line)) (scatter empres chn_pctile, msymbol(o) msize(small) mcolor(navy)), ///
			  title("") xtitle("Chinese Population Share in 2000, by Percentile",size(8pt)) ytitle("Pre-sample Employment, 1996-2000",size(8pt)) ///
			  xlab(,labsize(small)) ylab(,labsize(small)) ///
			  graphregion(color(white)) legend(off) /// 
			  saving("../results/emp_chnperc2.gph",replace) 
restore

preserve
reghdfe d_lnincome_9801 $X, absorb(countyname) res(incres)
collapse incres, by(chn_pctile)
graph twoway (lfitci incres chn_pctile,clstyle(p2line)) (scatter incres chn_pctile, msymbol(o) msize(small) mcolor(navy)), ///
			  title("") xtitle("Chinese Population Share in 2000, by Percentile",size(8pt)) ytitle("Pre-sample Income, 1996-2000",size(8pt)) ///
			  xlab(,labsize(small)) ylab(,labsize(small)) ///
			  graphregion(color(white)) legend(off) ///
			  saving("../results/inc_chnperc2.gph",replace) 
restore

graph combine "../results/emp_chnperc1.gph" "../results/inc_chnperc1.gph" "../results/emp_chnperc2.gph" "../results/inc_chnperc2.gph", graphregion(color(white)) 
graph export "../results/figureA7.pdf", replace 

